<?php
use think\Db;
use think\Cookie;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
// 定义一个函数getIP(),获取客户端ip地址
function getIP()
{
    global $ip;
    if (getenv("HTTP_CLIENT_IP"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("REMOTE_ADDR"))
        $ip = getenv("REMOTE_ADDR");
    else {
        $ip = "Unknow";
    }
    return $ip;
}





function daicg(){		//获取待采购单数量
    $cgcount = 0;
    $where=array(
        'cg_status'=>3,
    );
    $cg = Db::name('waimai_caigou_list')->field('cg_number')->where($where)->group('cg_number')->select();
    if(!empty($cg)){
        $cgcount = count($cg);
    }
   return $cgcount;
}

function daips(){		//获取待配送单数量
    $where=array(
        'order_status'=>3
    );
    $pscount=0;
    $pscount = Db::name('waimai_order')->where($where)->count();
     $cgcount = 0;
    $where=array(
        'cg_status'=>3,
    );
    $cg = Db::name('waimai_caigou_list')->field('cg_number')->where($where)->group('cg_number')->select();
    if(!empty($cg)){
        $cgcount = count($cg);
    }
   return $pscount;
}

function getmenu($order_id){

//获取每个订单的菜单名称和数量
$n = array(
    'order_id' => $order_id,
    'status' => 1
);
$menu=array();
$menu = Db::name('waimai_sell_log')->field('goods_id,name,price,guige,num,gkid,gwid')->where($n)->select();
    if (!empty($menu['0'])) {
       
        foreach ($menu as $key => $vo) {
            
           if(empty($vo['name'])){

                $goods = Db::name('waimai_goods')->field('goods_id,name,price,unit,gjson,gweight')->where('goods_id', $vo['goods_id'])->find();
                if (!empty($goods)) {
                 
                            $menu[$key]['name'] = $goods['name'];
                            $menu[$key]['price'] = $goods['price'];
                            //获取一元购的菜品规格一
                            $gjson = json_decode($goods['gjson'], true);

                            if (!empty($gjson[$vo['gkid'] - 1])) {
                                $menu[$key]['name'] = $gjson[$vo['gkid'] - 1]['0'];
                                $menu[$key]['price'] = $gjson[$vo['gkid'] - 1]['1'];
                            } else if (!empty($gjson['0'])) {
                                $menu[$key]['name'] = $gjson['0']['0'];
                                $menu[$key]['price'] = $gjson['0']['1'];
                            }

                            //获取一元购的菜品规格二
                            $gweight = json_decode($goods['gweight'], true);
               // dump($gweight);
                            if (!empty($gweight[$vo['gwid'] - 1])) {
                               // echo "not null 1:" .$vo['gwid'] ;
                                $menu[$key]['guige'] = $gweight[$vo['gwid'] - 1]['0'] . $gweight[$vo['gwid'] - 1]['1'];
                            } else {
                              //  echo "not null 3:". $vo['gwid'];
                                $menu[$key]['guige'] = $goods['unit'];
                            }
                    
                }
            }
        }
    }
  return $menu;
}

function gengxin_order(){
    $year=2016;
    $month=5;
    $first='01';
    for($i=0;$i<7;$i+=1){

        $days=cal_days_in_month(CAL_GREGORIAN, $month, $year);
        if($month<10){
            $str1=$year . "0". $month . $first;
            $str2=$year . "0". $month . $days;
        }else{
            $str1=$year . $month . $first;
            $str2=$year . $month . $days;
        }

        $firstday1=date("Y-m-d H:i:s",strtotime("$str1"));
        $lastday1=date("Y-m-d H:i:s",strtotime("$str2"));

        $firstday=strtotime($firstday1);		//生成时间戳
        $lastday=strtotime($lastday1)+86400;

        $wh=array(
            'create_time'=>[['<',$lastday],['>',$firstday],'and']
        );
        $orderlist=Db::name('waimai_order')->field('order_id,create_time,desc')->where($wh)->select();
        foreach($orderlist as $key=>$vo){
            if(empty($vo['ps_date']) || $vo['ps_date']==0 ){

                $ps_date = strtotime(date("Y-m-d", $vo['create_time']));
                $ps_time=10;
                if(!empty($vo['desc'])){
                    $str = $vo['desc'];
                    if(strpos($str, "9:00-10:00") !== false){
                        $ps_time = 9;
                    }else if(strpos($str, "10:00-11:00") !== false){
                        $ps_time = 10;
                    }else if(strpos($str, "11:00-12:00") !== false){
                        $ps_time = 11;
                    }else if(strpos($str, "15:00-16:00") !== false){
                        $ps_time = 15;
                    }else if(strpos($str, "16:00-17:00") !== false){
                        $ps_time = 16;
                    }else if(strpos($str, "17:00-18:00") !== false){
                        $ps_time = 17;
                    }
                }
                $update_wh=array(
                    'order_id'=>$vo['order_id']
                );
                $update_data=[
                    'ps_date'=>$ps_date,
                    'ps_time'=>$ps_time
                ];
                Db::name('waimai_order')->where($update_wh)->update($update_data);
            }
        }
        ++$month;

    }
   return 1;
    
}
/*根据商品数据表的图片字段的一段来得到图片*/
 function get_image_by_path($path,$image_type='-1'){
		if(!empty($path)){
			$image_tmp = explode(',',$path);
			if($image_type == '-1'){
				$return['image'] = '/upload/waimai/'.$image_tmp[0].'/'.$image_tmp['1'];
				$return['m_image'] = '/upload/waimai/'.$image_tmp[0].'/m_'.$image_tmp['1'];
				$return['s_image'] = '/upload/waimai/'.$image_tmp[0].'/s_'.$image_tmp['1'];
			}else{
				$return = '/upload/waimai/'.$image_tmp[0].'/'.$image_type.'_'.$image_tmp['1'];
			}
		
			return $return;
		}else{
			return false;
		}
	}
	
	/*根据商品数据表的图片字段来得到图片*/
	function get_allImage_by_path($path,$image_type='-1'){
		if(!empty($path)){
			$tmp_pic_arr = explode(';',$path);
			foreach($tmp_pic_arr as $key=>$value){
				$image_tmp = explode(',',$value);
				if($image_type == '-1'){
					$return[$key]['image'] = C('config.site_url').'/upload/waimai/'.$image_tmp[0].'/'.$image_tmp['1'];
					$return[$key]['m_image'] = C('config.site_url').'/upload/waimai/'.$image_tmp[0].'/m_'.$image_tmp['1'];
					$return[$key]['s_image'] = C('config.site_url').'/upload/waimai/'.$image_tmp[0].'/s_'.$image_tmp['1'];
				}else{
					$return[$key] = C('config.site_url').'/upload/waimai/'.$image_tmp[0].'/'.$image_type.'_'.$image_tmp['1'];
				}
			}
			return $return;
		}else{
			return false;
		}
	}
	/*根据商品数据表的自增ID字段来得到图片*/
	 function get_image_by_id($id,$image_type){
		$database_meal = D('Waimai_goods');
		$condition_meal['goods_id'] = $id;
		$now_meal = $database_meal->field('`image`')->where($condition_meal)->find();
		return $this->get_image_by_path($now_meal['image'],$image_type);
	}
	
	/*根据商品数据表的图片字段来删除图片*/
	 function del_image_by_path($path){
		if(!empty($path)){
			$image_tmp = explode(',',$path);
			unlink('./upload/waimai/'.$image_tmp[0].'/'.$image_tmp['1']);
			unlink('./upload/waimai/'.$image_tmp[0].'/m_'.$image_tmp['1']);
			unlink('./upload/waimai/'.$image_tmp[0].'/s_'.$image_tmp['1']);
			return true;
		}else{
			return false;
		}
	}
	/*根据商品数据表的自增ID字段来得到图片*/
	function del_image_by_id($id){
		$database_meal = D('Waimai_goods');
		$condition_meal['goods_id'] = $id;
		$now_meal = $database_meal->field('`image`')->where($condition_meal)->find();
		return $this->del_image_by_path($now_meal['image']);
	}


//function daicg()
//{
//	//今天待采购列表
//			$tomodate= strtotime(date("Y-m-d",strtotime("+1 day")));//明天日期
//				$where=array(
//					'cg_status'=>2,
//					'ps_date'=>['<',$tomodate]
//				);
//				$newcg_list = Db::name('waimai_caigou_list')->where($where)->select();
//			
//			return $newcg_list;
//}
//function daicg2()
//{
//	
//			
//			//明天，后天待采购列表
//			$tomodate2= strtotime(date("Y-m-d"));//今天日期
//				$where=array(
//					'cg_status'=>2,
//					'ps_date'=>['>',$tomodate2]
//				);
//				$newcg_list2 = Db::name('waimai_caigou_list')->where($where)->select();
//			
//				$view->assign('list2',$newcg_list2);
//				return $newcg_list2;
//}
